package com.ydd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ydd.dto.YddAdvimgDTO;
import com.ydd.dto.YddAdvimgPageDTO;
import com.ydd.entity.YddAdvimg;
import com.ydd.mapper.YddAdvimgMapper;
import com.ydd.result.PageResult;
import com.ydd.service.IYddAdvimgService;
import com.ydd.vo.YddAdvimgVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author lrc
 * @description 针对表【ydd_advimg】的数据库操作Service实现
 * @createDate 2023-03-16 18:24:00
 */
@Service
public class YddAdvimgServiceImpl implements IYddAdvimgService {

    @Autowired
    private YddAdvimgMapper yddAdvimgMapper;

    /**
     * 广告图分页查询
     *
     * @param yddAdvimgPageDTO
     * @return
     * @author ljs
     */
    @Override
    public PageResult pageQuery(YddAdvimgPageDTO yddAdvimgPageDTO) {
        PageHelper.startPage(yddAdvimgPageDTO.getPage(),yddAdvimgPageDTO.getLimit());
        Page<YddAdvimgVO> page = yddAdvimgMapper.pageQuery(yddAdvimgPageDTO);
        List<YddAdvimgVO> result = page.getResult();
        for (YddAdvimgVO yddAdvimgVO : result) {
            //查询广告图位置 advposname
            String advposname = yddAdvimgMapper.query(yddAdvimgVO.getAdvimgpos());
            yddAdvimgVO.setAdvposname(advposname);
        }
        return PageResult.builder()
                .count(page.getTotal())
                .data(page.getResult())
                .currentPage(yddAdvimgPageDTO.getPage())
                .totalPages(page.getPages())
                .pageSize(yddAdvimgPageDTO.getLimit())
                .build();
    }

    /**
     * 删除广告图
     *
     * @param id
     * @return
     */
    @Override
    public Boolean deleteById(Integer id) {
        YddAdvimg yddAdvimg = yddAdvimgMapper.getById(id);
        if("1".equals(yddAdvimg.getDefaultData())){
            return false;
        }
        yddAdvimgMapper.dropById(id);
        return true;
    }

    /**
     * 根据id查询广告图详情
     *
     * @param id
     * @return
     */
    @Override
    public YddAdvimg selectById(Integer id) {
        YddAdvimg yddAdvimg = yddAdvimgMapper.getById(id);
        return yddAdvimg;
    }

    /**
     * 修改广告图
     *
     * @param yddAdvimg
     */
    @Override
    public void update(YddAdvimg yddAdvimg) {
        yddAdvimgMapper.updateImg(yddAdvimg);
    }

    /**
     * 广告图添加
     *
     * @param yddAdvimgDTO
     */
    @Override
    public void add(YddAdvimgDTO yddAdvimgDTO) {
        YddAdvimg yddAdvimg = new YddAdvimg();
        BeanUtils.copyProperties(yddAdvimgDTO,yddAdvimg);
        yddAdvimg.setDefaultData(null);
        yddAdvimgMapper.add(yddAdvimg);
    }
}




